﻿/******************************************************************************************************
Copyright (c) 2014 Steven Giacomelli (stevegiacomelli@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************************************/

using System;
using System.Linq;

namespace SharpGeo.IO
{
    /// <summary>
    /// A static class to help parse little endian and big endian values
    /// </summary>
    public static class ByteConverter
    {
        //declare this as a static variable to avoid issues with the Debugger
        //changing the state of the IsLittleEndian flag
        private static bool _isLittleEndian = BitConverter.IsLittleEndian;

        /// <summary>
        /// Converts a byte array to a Double (Little Endian)
        /// </summary>
        /// <param name="value">The byte array to convert</param>
        /// <param name="index">The starting index</param>
        /// <returns>A Double converted from bytes (Little Endian)</returns>
        public static double ToLittleEndianDouble(byte[] value, int index)
        {
            if (value == null || index < 0 || index >= (int.MaxValue - 7) || (index + 7) >= value.Length)
                return double.NaN;

            var temp = new byte[] {
                value[index],
                value[index + 1],
                value[index + 2],
                value[index + 3],
                value[index + 4],
                value[index + 5],
                value[index + 6],
                value[index + 7]
            };

            if (!_isLittleEndian)
                temp.Reverse();

            return BitConverter.ToDouble(temp, 0);
        }

        /// <summary>
        /// Converts a byte array to a Double (Big Endian)
        /// </summary>
        /// <param name="value">The byte array to convert</param>
        /// <param name="index">The starting index</param>
        /// <returns>A Double converted from bytes (Big Endian)</returns>
        public static double ToBigEndianDouble(byte[] value, int index)
        {
            if (value == null || index < 0 || index >= (int.MaxValue - 7) || (index + 7) >= value.Length)
                return double.NaN;

            var temp = new byte[] {
                value[index+7],
                value[index + 6],
                value[index + 5],
                value[index + 4],
                value[index + 3],
                value[index + 2],
                value[index + 1],
                value[index ]
            };

            if (!_isLittleEndian)
                temp.Reverse();

            return BitConverter.ToDouble(temp, 0);
        }

        /// <summary>
        /// Converts a byte array to an Int32 (Little Endian)
        /// </summary>
        /// <param name="value">The byte array to convert</param>
        /// <param name="index">The starting index</param>
        /// <returns>An Int32 converted from bytes (Little Endian)</returns>
        public static int ToLittleEndianInt32(byte[] value, int index)
        {
            if (value == null || index < 0 || index >= (int.MaxValue - 3) || (index + 3) >= value.Length)
                return int.MinValue;

            unchecked
            {
                return
                    ((int)value[index + 3] << 24) +
                    ((int)value[index + 2] << 16) +
                    ((int)value[index + 1] << 8) +
                    ((int)value[index]);
            }
        }

        /// <summary>
        /// Converts a byte array to an Int32 (Big Endian)
        /// </summary>
        /// <param name="value">The byte array to convert</param>
        /// <param name="index">The starting index</param>
        /// <returns>An Int32 converted from bytes (Big Endian)</returns>
        public static int ToBigEndianInt32(byte[] value, int index)
        {
            if (value == null || index < 0 || index >= (int.MaxValue - 3) || (index + 3) >= value.Length)
                return int.MinValue;

            unchecked
            {
                return
                    ((int)value[index] << 24) +
                    ((int)value[index + 1] << 16) +
                    ((int)value[index + 2] << 8) +
                    ((int)value[index + 3]);
            }
        }

        /// <summary>
        /// Converts a byte array to an Int16 (Little Endian)
        /// </summary>
        /// <param name="value">The byte array to convert</param>
        /// <param name="index">The starting index</param>
        /// <returns>An Int16 converted from bytes (Little Endian)</returns>
        public static int ToLittleEndianInt16(byte[] value, int index)
        {
            if (value == null || index < 0 || index >= (int.MaxValue - 1) || (index + 1) >= value.Length)
                return int.MinValue;

            unchecked
            {
                return ((int)value[index + 1] << 8) + ((int)value[index]);
            }
        }

        /// <summary>
        /// Converts a byte array to an Int16 (Big Endian)
        /// </summary>
        /// <param name="value">The byte array to convert</param>
        /// <param name="index">The starting index</param>
        /// <returns>An Int16 converted from bytes (Big Endian)</returns>
        public static int ToBigEndianInt16(byte[] value, int index)
        {
            if (value == null || index < 0 || index >= (int.MaxValue - 1) || (index + 1) >= value.Length)
                return int.MinValue;

            unchecked
            {
                return ((int)value[index] << 8) + ((int)value[index + 1]);
            }
        }
    }
}